package com.klasster.recruitmentPlatform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.klasster.recruitmentPlatform.common.response.Result;
import com.klasster.recruitmentPlatform.common.response.ResultCode;
import com.klasster.recruitmentPlatform.common.util.ResumeSubmissionUtil;
import com.klasster.recruitmentPlatform.domain.dto.InterviewDTO;
import com.klasster.recruitmentPlatform.domain.dto.ResumeSubmissionStepDTO;
import com.klasster.recruitmentPlatform.domain.po.*;
import com.klasster.recruitmentPlatform.domain.vo.JobVO;
import com.klasster.recruitmentPlatform.domain.vo.RecruiterSelectionVO;
import com.klasster.recruitmentPlatform.service.impl.*;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/recruiter")
public class RecruiterController {

    private final RecruiterServiceImpl recruiterService;
    private final JobServiceImpl jobService;
    private final CompanyServiceImpl companyService;

    private final ResumeSubmissionUtil resumeSubmissionUtil;

    @GetMapping("/getRecruiter/{recruiter_id}")
    public Result getRecruiter(@PathVariable Long recruiter_id) {
        Recruiter recruiter = recruiterService.getById(recruiter_id);
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,recruiter);
    }

    @Transactional
    @PutMapping("/updateRecruiter")
    public Result updateRecruiter(@RequestBody Recruiter recruiter) {
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,recruiterService.updateById(recruiter));
    }

    /**
     * 获取招聘者审核招聘信息
     */
    @GetMapping("/getRecruiterJobPosting/{recruiter_id}")
    public Result getRecruiterJobPosting(@PathVariable Long recruiter_id) {
        //只返回state为0、1和2的招聘信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruiter_id",recruiter_id).and(i -> i.eq("state",0).or().eq("state",1).or().eq("state",2));
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,jobService.list(queryWrapper));
    }

    /**
     * 获取审核通过之后的招聘信息
     */
    @GetMapping("/getRecruiterAuditedJobPosting/{recruiter_id}")
    public Result getRecruiterAuditedJobPosting(@PathVariable Long recruiter_id) {
        //只返回state不为0和2的招聘信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruiter_id",recruiter_id).ne("state",0).ne("state",2);
        List<JobVO> list = BeanUtil.copyToList(jobService.list(queryWrapper),JobVO.class);
        list.forEach(e -> {
            Company company = companyService.getById(e.getCompany_id());
            e.setCompany_name(company.getCompany_name());
        });
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,list);
    }

    @Transactional
    @PostMapping("/saveJobPosting")
    public Result saveJobPosting(@RequestBody Job job) {
        job.setState(0);
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,jobService.save(job));
    }

    @Transactional
    @PutMapping("/updateJobPosting")
    public Result updateJobPosting(@RequestBody Job job) {
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,jobService.updateById(job));
    }

    @Transactional
    @PutMapping("/deleteJobPosting/{job_id}")
    public Result deleteJobPosting(@PathVariable Long job_id) {
        //设置为删除撤销状态
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",job_id);
        updateWrapper.set("state",3);
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,jobService.update(updateWrapper));
    }

    /**
     * 获取简历投递列表
     */
    @GetMapping("/getResumeSubmissions/{recruiter_id}")
    public Result getResumeSubmissions(@PathVariable Long recruiter_id) {
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,resumeSubmissionUtil.getResumeSubmissions("recruiter_id",recruiter_id));
    }

    /**
     * 获取企业内部人员选择列表
     */
    @GetMapping("/getCompanyRecruiters/{company_id}")
    public Result getCompanyRecruiters(@PathVariable Long company_id) {
        QueryWrapper<Recruiter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",company_id);
        List<Recruiter> recruiters = recruiterService.list(queryWrapper);
        List<RecruiterSelectionVO> recruiterSelectionVOS = BeanUtil.copyToList(recruiters,RecruiterSelectionVO.class);
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,recruiterSelectionVOS);
    }

    /**
     * 获取招聘人员面试安排列表
     */
    @GetMapping("/getInterviewArrangements/{recruiter_id}")
    public Result getInterviewArrangements(@PathVariable Long recruiter_id) {
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,resumeSubmissionUtil.getInterviewArrangements(recruiter_id));
    }

    /**
     * 通过面试
     */
    @Transactional
    @PutMapping("/passInterview")
    public Result passInterview(@RequestBody ResumeSubmissionStepDTO resumeSubmissionStepDTO) {
        return resumeSubmissionUtil.passInterview(resumeSubmissionStepDTO);
    }

    /**
     * 不通过面试
     */
    @Transactional
    @PutMapping("/failInterview")
    public Result failInterview(@RequestBody ResumeSubmissionStepDTO resumeSubmissionStepDTO) {
        return resumeSubmissionUtil.failInterview(resumeSubmissionStepDTO);
    }

    /**
     * 安排面试
     */
    @Transactional
    @PostMapping("/arrangeInterview")
    public Result arrangeInterview(@RequestBody InterviewDTO interviewDTO) {
        return resumeSubmissionUtil.arrangeInterview(interviewDTO);
    }

    /**
     * 获取简历投递进展
     */
    @GetMapping("/getResumeSubmissionProgress")
    public Result getResumeSubmissionProgress(@RequestParam Long submission_id) {
        return Result.success(ResultCode.RECRUITER_GET_SUCCESS,resumeSubmissionUtil.getResumeSubmissionProgress(submission_id));
    }


}
